package com.xm.dao.impl;

import com.xm.dao.UsersDao;
import com.xm.po.Users;
import com.xm.util.BaseDao;
import com.xm.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xie'lin'feng
 * @date 2020-08-07-11:05
 * @description 用户数据操作接口实现类
 */
public class UsersDaoImpl implements UsersDao {
    @Override
    public Users getUserByName(String name) {
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Users users = null;
        String sql = "select * from users where loginName = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,name);
            rs = ps.executeQuery();
            users = (Users) DButil.getRsOne(Users.class,rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return users;
    }
    /**
     * 根据name和id查询用户信息
     * @param name
     * @param id
     * @return
     */
    @Override
    public List<Users> getUserByName(String name, String id) {
        List<Users> usersList = new ArrayList<>();
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        //根据id和name查询用户信息
        String sql = "SELECT * FROM users WHERE userId=? OR loginName like ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,id);
            ps.setObject(2,name);
            rs = ps.executeQuery();
            usersList = (List<Users>) DButil.getRsData(Users.class, rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return usersList;
    }

    @Override
    public List<Users> getUsersByCondition(Users users) {
        return null;
    }

    @Override
    public Integer activeUserCount() {
        return null;
    }
    /**
     * 新增用户
     * @param users
     * @return
     */
    @Override
    public boolean addUsers(Users users) {
        boolean add = BaseDao.add(users);
        return add;
    }

    @Override
    public boolean updateLastLoginTime(Users users) {
        return DButil.update(users);
    }

    @Override
    public boolean checkUser(String name) {
        return false;
    }

    /**
     * 修改用户信息
     * @param users
     * @return
     */
    @Override
    public boolean update(Users users) {
        return DButil.update(users);
    }

    /**
     * 根据手机号查询用户
     * @param phone
     * @return
     */
    @Override
    public Users usersByPhone(String phone) {
        Users users = new Users();
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT userPhone FROM `users` WHERE userPhone=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,phone);
            rs = ps.executeQuery();
            if (rs.next()){
                users.setUserPhone(rs.getString("userPhone"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.close(rs,ps,conn);
        }
        return users;
    }
    /**
     * 查询所有用户信息
     * @return
     */
    @Override
    public List<Users> userQueryAll() {
        List<Users> user = (List<Users>) DButil.queryAll(Users.class);
        return user;
    }
    /**
     * 根据id查询用户
     * @return
     */
    @Override
    public Users queryByOne(Integer id) {
        Users users = (Users) DButil.queryOne(Users.class, id);

        return users;
    }

    /**
     * 根据id修改用户信息(冻结)
     * @param id
     * @return
     */
    @Override
    public boolean updateById(Integer id) {
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        String sql = "UPDATE `users` SET userStatus=2 WHERE userId=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,id);
            int i = ps.executeUpdate();
            return i>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 根据id修改用户信息(解冻)
     * @param id
     * @return
     */    @Override
    public boolean updateByid(Integer id) {
        Connection conn = DButil.getConnection();
        PreparedStatement ps = null;
        String sql = "UPDATE `users` SET userStatus=1 WHERE userId=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,id);
            int i = ps.executeUpdate();
            return i>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
